package cn.com.jrr.core.controller.dictionary;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.dictionary.Code;
import cn.com.jrr.core.model.dictionary.CodeType;
import cn.com.jrr.core.service.dictionary.CodeService;
import cn.com.jrr.core.service.dictionary.CodeTypeService;

@Controller
@RequestMapping("/syscode")
public class CodeAction {
    @Autowired
    private CodeTypeService codeTypeService;
    @Autowired
    private CodeService codeService;
    private DataTableResponse<Code> dt;
    @Autowired
    private ResponseMessage responseMessage;


    @RequestMapping("/index")
    public String index(){
        return "/dictionary/codemain";
    }
    @ResponseBody
    @RequestMapping("/list")
    public DataTableResponse<Code> list(DataTableRequest dataTableRequest){
        dt=codeService.getAll(dataTableRequest);
        return dt;
    }
    @ResponseBody
    @RequestMapping("/getDateByCode")
    public List<CodeType> getDateByCode(@RequestParam String code){
        List<CodeType> list=codeTypeService.listByCode(code);
        return list;
    }
    @ResponseBody
    @RequestMapping("/addAndUpdate")
    public ResponseMessage addUpdate(Code code){
        if(code!=null){
            int res=0;
            if(code.getId()!=null){
                //更新
                res=codeService.update(code);
            }else{
                //添加
                Code cd=codeService.getByCode(code.getCode());
                if(cd!=null){
                    responseMessage.setMessage("该编码已经存在！");
                }else{
                    res=codeService.add(code);
                }
            }
            if(res<=0){
                responseMessage.setStatus("fail");
            }
            responseMessage.setStatus("success");
        }else{
            responseMessage.setStatus("fail");
        }

        return responseMessage;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseMessage demoDelete(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            codeService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * get
     */
    @ResponseBody
    @RequestMapping("/get")
    public Code getByID(@RequestParam Integer id){
        Code code=codeService.get(id);
        return code;
    }

    @ResponseBody
    @RequestMapping("/codetypelist")
    public DataTableResponse<CodeType> codeTypeList(DataTableRequest dataTableRequest,String code){
        DataTableResponse<CodeType> dtt=codeTypeService.getByCode(dataTableRequest,code);
        return dtt;
    }
    @ResponseBody
    @RequestMapping("/codetypeadd")
    public ResponseMessage codeTypeAdd(CodeType codeType){
        if(codeType!=null){
            int res=0;
            if(codeType.getId()!=null){
                //更新
                res=codeTypeService.update(codeType);
            }else{
                //添加
                int result=codeTypeService.getByCodeAndType(codeType);
                if(result>0){
                    responseMessage.setMessage("该编码已经存在！");
                }else{
                    res=codeTypeService.add(codeType);
                }
            }
            if(res<=0){
                responseMessage.setStatus("fail");
            }
            responseMessage.setStatus("success");
        }else{
            responseMessage.setStatus("fail");
        }

        return responseMessage;
    }
    /**
     * getCodeTypeById
     */
    @ResponseBody
    @RequestMapping("/getCodeTypeById")
    public CodeType getCodeTypeById(@RequestParam Integer id){
        CodeType codeType=codeTypeService.getById(id);
        return codeType;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/deleteCodeType")
    public ResponseMessage deleteCodeType(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            codeTypeService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
}
